﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EM.FrameWork.Paging
{
    /// <summary>
    /// 分页列表继承自列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PagedList<T> : List<T>
    {
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="items">列表项</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">每页条数</param>
        public PagedList(IList<T> items, int pageIndex, int pageSize)
        {
            PageSize = pageSize;
            TotalItemCount = items.Count;
            PageIndex = pageIndex;
            for (int i = StartRecordIndex - 1; i < EndRecordIndex; i++)
            {
                Add(items[i]);
            }
        }
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="items">列表项</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">页面条数</param>
        /// <param name="totalItemCount">总条数</param>
        public PagedList(IEnumerable<T> items, int pageIndex, int pageSize, int totalItemCount)
        {
            AddRange(items);
            TotalItemCount = totalItemCount;
            PageIndex = pageIndex;
            PageSize = pageSize;
        }
        /// <summary>
        /// 页数
        /// </summary>
        public int PageIndex { get; set; }
        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalItemCount { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPageCount { get { return (int)Math.Ceiling(TotalItemCount / (double)PageSize); } }
        /// <summary>
        /// 开始记录位置
        /// </summary>
        public int StartRecordIndex { get { return (PageIndex - 1) * PageSize + 1; } }
        /// <summary>
        /// 结束记录位置
        /// </summary>
        public int EndRecordIndex { get { return TotalItemCount > PageIndex * PageSize ? PageIndex * PageSize : TotalItemCount; } }
    }
}
